
import i18n from '../utils/lang/index'
/*
全局公共变量和函数
 */
// exports.install = function (Vue, options) {
//
//
// };
export default {
  install(Vue) {
    /*
 标签种类
  */
    Vue.prototype.v_TagTypes = [
      { value: 'total', label: '全部' },
      { value: '0', label: '人员' },
      { value: '1', label: '车辆' },
      { value: '2', label: '物材' }
    ]

    /*
    告警种类
     */
    // Vue.prototype.v_AlarmTypes = [
    //   { value: '', label: '全部' },
    //   { value: 'alarmIn', label: '非法进入' },
    //   { value: 'alarmOut', label: '非法离开' },
    //   { value: 'SOS', label: 'SOS告警' },
    //   { value: 'timeOut', label: '超时告警' },
    //   { value: 'gatherOver', label: '超员告警' },
    //   { value: 'fallDown', label: '跌倒告警' },
    //   { value: 'strapBroken', label: '腕带断裂' },
    //   { value: 'unHelmet', label: '未戴安全帽' },
    //   { value: 'unWorkClothes', label: '未穿工作服' },
    //   { value: 'lowBattery', label: '低电量告警' },
    //   { value: 'lackOfStaff', label: '缺员告警' },
    //   { value: 'static', label: '静止告警' },
    //   { value: 'stay', label: '滞留告警' }
    // ]

    // /*
    // 告警状态
    //  */
    // Vue.prototype.v_AlarmStatuss = [
    //   { value: '', label: '全部' },
    //   { value: '未处理', label: '未处理' },
    //   { value: '已处理', label: '已处理' },
    //   { value: '超时', label: '超时' }
    // ]

    /*
    * 快速设置时间
    * */
    Vue.prototype.v_PickerOptions = {
      disabledOptions(time) { // 控制大于当前日期的日期不可用
        return time.getTime() > Date.now()
      },
      shortcuts: [
        {
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date())
          }
        },
        {
          text: '昨天',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', date)
          }
        },
        {
          text: '一周前',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        }]
    }

    /**
     * 快速设置日期时间格式
     */
    Vue.prototype.v_DateTimePickerOptions = {
      shortcuts: [
        {
          text: i18n.t('timePlugin.today'),
          onClick(picker) {
            const start = new Date()

            const year = start.getFullYear()
            const month = start.getMonth()
            const day = start.getDate()

            picker.$emit('pick', [new Date(year, month, day, '0', '0', '0'), new Date(year, month, day, '23', '59', '59')])
          }
        },
        {
          text: i18n.t('timePlugin.yesterday'),
          onClick(picker) {
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24)

            const year = start.getFullYear()
            const month = start.getMonth()
            const day = start.getDate()

            picker.$emit('pick', [new Date(year, month, day, '0', '0', '0'), new Date(year, month, day, '23', '59', '59')])
          }
        },
        {
          text: i18n.t('timePlugin.lastWeek'),
          onClick(picker) {
            const end = new Date()
            end.setTime(end.getTime() - 3600 * 1000 * 24 * 1)
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)

            const end_year = end.getFullYear()
            const end_month = end.getMonth()
            const end_day = end.getDate()
            const start_year = start.getFullYear()
            const start_month = start.getMonth()
            const start_day = start.getDate()

            picker.$emit('pick', [new Date(start_year, start_month, start_day, '0', '0', '0'), new Date(end_year, end_month, end_day, '23', '59', '59')])
          }
        },
        {
          text: i18n.t('timePlugin.lastMonth'),
          onClick(picker) {
            const end = new Date()
            end.setTime(end.getTime() - 3600 * 1000 * 24 * 1)
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            const end_year = end.getFullYear()
            const end_month = end.getMonth()
            const end_day = end.getDate()
            const start_year = start.getFullYear()
            const start_month = start.getMonth()
            const start_day = start.getDate()
            picker.$emit('pick', [new Date(start_year, start_month, start_day, '0', '0', '0'), new Date(end_year, end_month, end_day, '23', '59', '59')])
          }
        },
        {
          text: i18n.t('timePlugin.notSet'),
          onClick(picker) {
            const end = new Date()
            const end_year = end.getFullYear()
            const end_month = end.getMonth()
            const end_day = end.getDate()
            picker.$emit('pick', [new Date('2011', '12', '01', '0', '0', '0'), new Date(end_year, end_month, end_day, '23', '59', '59')])
          }
        }
      ]
    }

    /*
    * 地图数据设置
    * */
    Vue.prototype.v_MapTreeData = [
      {
        id: 1,
        label: '通讯',
        children: [
          {
            id: 11,
            label: '研发部一部'
          },
          {
            id: 12,
            label: '研发部二部'
          },
          {
            id: 13,
            label: '研发部三部'
          }
        ]
      }
    ]
    Vue.prototype.v_MapTreeDataSel = [
      { value: 1, label: '通讯' },
      { value: 11, label: '通讯-研发部一部' },
      { value: 12, label: '通讯-研发部二部' },
      { value: 13, label: '通讯-研发部三部' },
      { value: 21, label: '通讯-客户服务部' }
    ]

    Vue.prototype.v_DefaultProps = {
      children: 'children',
      label: 'label'
    }
    /*
    * 消息弹出框
    * */
    Vue.prototype.v_MyMessage = function(msg, type) {
      this.$message({
        message: msg,
        center: true,
        type: type,
        duration: 1500
      })
    }

    /**
     * 告警弹出框
     */
    // Vue.prototype.openWarningFrame = function(title, message) {
    //   this.$notify({
    //     title: title,
    //     message: message,
    //     type: "warning", //弹框的种类success/warning/info/error
    //     duration: 3000,
    //     position: "top-right",
    //     offset: 400
    //   });
    // },

    /*
    * fuserid列表
    * */
    Vue.prototype.v_FuserID = [
      { value: 'LCID11111111', label: 'LCID11111111' },
      { value: 'LCID22222222', label: 'LCID22222222' },
      { value: 'LCID33333333', label: 'LCID33333333' },
      { value: 'LCID44444444', label: 'LCID44444444' },
      { value: 'LCID55555555', label: 'LCID55555555' }
    ]

    /**
     * 摄像头列表
     */
    Vue.prototype.v_CameraCfg = [
      { ip: '192.168.1.146', name: '空调', username: 'admin', password: 'woxu123456', coordinate: '100, 100, 5', layer: '1' },
      { ip: '192.168.1.148', name: '绿萝', username: 'admin', password: 'woxu123456', coordinate: '50, 50, 5', layer: '1' }
    ]

    // 获取当前日期
    Vue.prototype.v_GetCurrentDate = function() {
      const myDate = new Date()
      const date = myDate.getFullYear() + '-' + (Array(2).join('0') + (myDate.getMonth() + 1)).slice(-2) + '-' + (Array(2).join('0') + myDate.getDate()).slice(-2)
      return date
    }

    Vue.prototype.v_GetAWeekAgoDate = function() {
      const myDate = new Date((new Date()).getTime() - 1000 * 60 * 60 * 24 * 6)

      const date = myDate.getFullYear() + '-' + (Array(2).join('0') + (myDate.getMonth() + 1)).slice(-2) + '-' + (Array(2).join('0') + myDate.getDate()).slice(-2)
      return date
    }

    /**
     * 获取当前日期和时间
     */
    Vue.prototype.v_GetCurrentDateTime = function() {
      const date = new Date()
      const formatdt = date.getFullYear() + '-' +
        ((date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + '-' +
        (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ' +
        (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' +
        (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':' +
        (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
      return formatdt
    }

    /**
     * 格式化日期和时间
     */
    Vue.prototype.v_FormatDateTime = function(date) {
      const strdate = new Date(date)
      const formatdt = strdate.getFullYear() + '-' +
        ((strdate.getMonth() + 1) < 10 ? '0' + (strdate.getMonth() + 1) : (strdate.getMonth() + 1)) + '-' +
        (strdate.getDate() < 10 ? '0' + strdate.getDate() : strdate.getDate()) + ' ' +
        (strdate.getHours() < 10 ? '0' + strdate.getHours() : strdate.getHours()) + ':' +
        (strdate.getMinutes() < 10 ? '0' + strdate.getMinutes() : strdate.getMinutes()) + ':' +
        (strdate.getSeconds() < 10 ? '0' + strdate.getSeconds() : strdate.getSeconds())
      return formatdt
    }

    // Vue.prototype.v_FormatDateTimeEx = function (date) {
    //   let strdate = new Date(date);
    //   let formatdt = strdate.getFullYear() + "-" +
    //     ((strdate.getMonth() + 1) < 10 ? "0" + (strdate.getMonth() + 1) : (strdate.getMonth() + 1)) + "-" +
    //     (strdate.getDate() < 10 ? "0" + strdate.getDate() : strdate.getDate()) + " " +
    //     (strdate.getHours() < 10 ? "0" + strdate.getHours() : strdate.getHours()) + ":" +
    //     (strdate.getMinutes() < 10 ? "0" + strdate.getMinutes() : strdate.getMinutes()) + ":" +
    //     (strdate.getSeconds() < 10 ? "0" + strdate.getSeconds() : strdate.getSeconds());
    //   return formatdt;
    // }

    /**
     * 验证账号和密码
     */
    Vue.prototype.validateUser = function(from) {
      if (from.username != '' && from.password != '') {
        // 先验证账号，判断
        if (from.username === 'root') {
          if (from.password === 'OneTwo34Five') {
            return true
          } else {
            this.v_MyMessage('密码错误', 'error')
          }
        } else if (from.username === 'admin') {
          if (from.password === 'admin') {
            return true
          } else {
            this.v_MyMessage('密码错误', 'error')
          }
        } else if (from.username === 'tunnel') {
          if (from.password === 'tunnel') {
            return true
          } else {
            this.v_MyMessage('密码错误', 'error')
          }
        } else if (from.username === 'guest') {
          if (from.password === 'guest') {
            return true
          } else {
            this.v_MyMessage('密码错误', 'error')
          }
        } else {
          this.v_MyMessage('此账号不存在', 'warning')
        }
      } else {
        this.v_MyMessage('账号和密码都不能为空', 'error')
      }

      return false
    }

    /**
     * 生成随机RGB值
     */
    Vue.prototype.randomRGB = function() {
      const r = Math.round(Math.random() * 255)
      const g = Math.round(Math.random() * 255)
      const b = Math.round(Math.random() * 255)

      return '#' + (r.toString(16).length > 1 ? r.toString(16) : '0' + r.toString(16)) +
        (g.toString(16).length > 1 ? g.toString(16) : '0' + g.toString(16)) +
        (b.toString(16).length > 1 ? b.toString(16) : '0' + b.toString(16))
    }

    /**
     * 网络请求响应处理封装
     */
    Vue.prototype.responseUse = function(res, b = false) {
      const code = res.code
      if (typeof code === 'undefined') {
        this.v_MyMessage(i18n.t('Notice.noStatusCode'), 'error')
      } else if (code == 200) {
        if (b) {
          this.v_MyMessage(res.msg, 'success')
          return true
        }
        return res.data
      } else if (code == 500) {
        this.v_MyMessage(res.msg, 'error')
        return false
      } else if (code == 401) {
        this.v_MyMessage(i18n.t('Notice.permissionDenied'), 'warning')
      } else if (code == 404) {
        this.v_MyMessage(i18n.t('Notice.noResourcesFound'), 'info')
      } else if (code == 707) { // token老化，或者token错误
        this.Error707()
      }
    }

    Vue.prototype.Error707 = function() {
      this.$router.push('/login')
      this.v_MyMessage(i18n.t('Notice.tokenError'), 'error')
    }
    /**
     * 获取一组数据中某个字段有哪些值
     */
    Vue.prototype.resourceFilter = function(data, type) {
      const filtertype = new Array()
      let b = false
      for (let i = 0; i < data.length; i++) {
        const value = data[i][type]

        b = false
        for (let j = 0; j < filtertype.length; j++) {
          if (filtertype[j].text === value) {
            b = true
            break
          }
        }

        // filtertype中没有当前类型
        if (!b) {
          const obj = { text: value, value: value }
          filtertype.push(obj)
        }
      }

      return filtertype
    }
  }
}

